package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.beauty.domain.BeautyServiceStore;
import com.linq.cool.beauty.dto.BeautyServiceStoreQueryDTO;
import com.linq.cool.beauty.service.BeautyServiceStoreService;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/13 22:17
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("serviceStore")
public class BeautyServiceStoreController extends BaseController {
    @Autowired
    private BeautyServiceStoreService beautyServiceStoreService;

    @PreAuthorize(hasPerm = "beauty:serviceStore:list")
    @GetMapping("list")
    public Result<?> list(BeautyServiceStoreQueryDTO dto) {
        IPage<BeautyServiceStore> page = beautyServiceStoreService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }


    @GetMapping(value = "{serviceStoreId}")
    public Result<?> getInfo(@PathVariable("serviceStoreId") Long serviceStoreId) {
        return Result.ok(beautyServiceStoreService.getById(serviceStoreId));
    }

    @Log(moduleTitle = "店面管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "beauty:serviceStore:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody BeautyServiceStore serviceStore) {
        serviceStore.setCreator(SecurityUtils.getUserId());
        return this.toResult(beautyServiceStoreService.saveServiceStore(serviceStore));
    }

    @Log(moduleTitle = "店面管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:serviceStore:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody BeautyServiceStore serviceStore) {
        serviceStore.setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(beautyServiceStoreService.updateServiceStore(serviceStore));
    }


    @Log(moduleTitle = "车辆管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "beauty:serviceStore:remove")
    @DeleteMapping("{serviceStoreIds}")
    public Result<?> remove(@PathVariable("serviceStoreIds") List<Long> serviceStoreIds) {
        return this.toResult(beautyServiceStoreService.removeByIds(serviceStoreIds));
    }

    /**
     * 根据省市区查询 服务店面
     */
    @PostMapping("getServiceStoresByArea")
    public Result<?> getServiceStoresByArea(@RequestBody BeautyServiceStoreQueryDTO dto) {
        List<BeautyServiceStore> list = beautyServiceStoreService.list(new LambdaQueryWrapper<BeautyServiceStore>()
                                                                               .eq(BeautyServiceStore::getProvince, dto.getProvince())
                                                                               .eq(BeautyServiceStore::getCity, dto.getCity())
                                                                               .eq(BeautyServiceStore::getCounty, dto.getCounty())
        );
        return Result.ok(list);
    }
}
